﻿using System;
using System.IO;
using System.Reflection;
using __Ts.Core.Log;

namespace __Ts.Core.Plugin
{
    public class RemoteLoader : MarshalByRefObject
    {
        private static IPlugin _plugin = null;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="assemblyPath"></param>
        /// <returns></returns>
        public IPlugin LoadPlugin(string assemblyPath)
        {
            try
            {
                if (_plugin == null)
                {
                    Assembly _assembly = Assembly.Load(AssemblyName.GetAssemblyName(assemblyPath));
                    string assemblyName = Path.GetFileNameWithoutExtension(assemblyPath);
                    string classPath = assemblyName + ".Plugin";
                    Type type = _assembly.GetType(classPath);
                    _plugin = (IPlugin)type.InvokeMember("GetInstance", BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy | BindingFlags.InvokeMethod, null, null, null);
                }
                return _plugin;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
            }
            return null;
        }
        /// <summary>
        /// 远程对象生命周期
        /// </summary>
        /// <returns></returns>
        public override object InitializeLifetimeService()
        {
            return null;
        }
    }
}
